package interceptor;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import pojo.SCMUser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 登录拦截器
 *
 * @ClassName 刘星宇
 * @Description TODO
 * @date 2022/11/23 15:03
 * @Version 1.0
 */
public class LoginInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {


        System.out.println("...LoginInterceptor...");
        // 如果不是映射到方法直接通过
        if (!(object instanceof HandlerMethod)) {
            return true;
        }
            SCMUser scmUser = (SCMUser) request.getSession().getAttribute("loginUser");

        if (scmUser != null) {
            return true;
        } else {
            response.sendRedirect(request.getContextPath() + "/login_in.html");
            return false;
        }
    }
}
